package com.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class Perpustakaan implements Serializable {

    public static String NAMA = "Perpustakaan Kota";
    public static ArrayList<Publikasi> DAFTAR_PUBLIKASI = new ArrayList<Publikasi>();
    public static ArrayList<Anggota> DAFTAR_ANGGOTA = new ArrayList<Anggota>();
    public static ArrayList<Pinjaman> DAFTAR_PINJAMAN = new ArrayList<Pinjaman>();
    private TimerBacaPublikasi TIMER_BACA_PUBLIKASI;
    private TimerSimpanPublikasi TIMER_SIMPAN_PUBLIKASI;

    public static void tambahPublikasi(Publikasi publikasi) {
        DAFTAR_PUBLIKASI.add(publikasi);
    }

    public static void tambahAnggota(Anggota anggota) {
        DAFTAR_ANGGOTA.add(anggota);
    }

    public static void tambahPinjaman(Pinjaman pinjaman) {
        DAFTAR_PINJAMAN.add(pinjaman);
    }

    public static String bacaDaftarPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < 10; i++) {
                if (i < DAFTAR_PUBLIKASI.size()) {
                    hasil += "Judul        : " + DAFTAR_PUBLIKASI.get(i).getJudul() + "\n";
                    hasil += "ID Koleksi   : " + DAFTAR_PUBLIKASI.get(i).getIdKoleksi() + "\n";
                    hasil += "Penerbit     : " + DAFTAR_PUBLIKASI.get(i).getPenerbit() + "\n";
                    hasil += "Penulis   `  : " + DAFTAR_PUBLIKASI.get(i).getPenulis() + "\n";
                    hasil += "Tahun        : " + DAFTAR_PUBLIKASI.get(i).getTahun() + "\n\n";
                } else {
                    return hasil;
                }
            }
        }
        return "kosong";
    }

    public static String bacaDaftarAnggota() {
        if (DAFTAR_ANGGOTA.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < 10; i++) {
                if (i < DAFTAR_ANGGOTA.size()) {
                    hasil += "Nama      : " + DAFTAR_ANGGOTA.get(i).getNama() + "\n";
                    hasil += "ID Anggota: " + DAFTAR_ANGGOTA.get(i).getIdAnggota() + "\n";
                } else {
                    return hasil;
                }
            }
        }
        return "kosong";
    }

    public static String bacaDaftarPinjaman() {
        if (DAFTAR_PINJAMAN.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < 10; i++) {
                if (i < DAFTAR_PINJAMAN.size()) {
                    hasil += "Nama           : " + DAFTAR_PINJAMAN.get(i).getPeminjam().getNama() + "\n";
                    hasil += "ID Anggota     : " + DAFTAR_PINJAMAN.get(i).getPeminjam().getIdAnggota() + "\n";
                    hasil += "Judul          : " + DAFTAR_PINJAMAN.get(i).getPublikasi().getJudul() + "\n";
                    hasil += "ID Koleksi     : " + DAFTAR_PINJAMAN.get(i).getPublikasi().getIdKoleksi() + "\n";
                    hasil += "Penerbit       : " + DAFTAR_PINJAMAN.get(i).getPublikasi().getPenerbit() + "\n";
                    hasil += "Penulis        : " + DAFTAR_PINJAMAN.get(i).getPublikasi().getPenulis() + "\n";
                    hasil += "Tanggal Pinjam : " + DAFTAR_PINJAMAN.get(i).getTanggalPinjam().getDate() + "\n";
                    hasil += "Tanggal Kembali: " + DAFTAR_PINJAMAN.get(i).getTanggalKembali().getDate() + "\n";
                    hasil += "Denda          : " + DAFTAR_PINJAMAN.get(i).getPublikasi().hitungDenda(10) + "\n";
                } else {
                    return hasil;
                }
            }
        }
        return "kosong";
    }

    public static void simpanFilePublikasi() {
        FileOutputStream outStream = null;
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {
            File outFile = new File("Publikasi.dat");
            try {
                outStream = new FileOutputStream(outFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            ObjectOutputStream outObjectStream = null;
            try {
                outObjectStream = new ObjectOutputStream(outStream);
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                outObjectStream.writeObject(DAFTAR_PUBLIKASI);
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                outStream.close();
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaFilePublikasi() {
        FileInputStream inStream = null;
        File inFile = new File("Publikasi.dat");
        try {
            inStream = new FileInputStream(inFile);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        ObjectInputStream inObjectStream = null;
        try {
            inObjectStream = new ObjectInputStream(inStream);
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            DAFTAR_PUBLIKASI = (ArrayList<Publikasi>) inObjectStream.readObject();
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void simpanFileAnggota() {
        FileOutputStream outStream = null;
        if (DAFTAR_ANGGOTA.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Anggota Kosong");
        } else {
            File outFile = new File("Anggota.dat");
            try {
                outStream = new FileOutputStream(outFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            ObjectOutputStream outObjectStream = null;
            try {
                outObjectStream = new ObjectOutputStream(outStream);
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            String data = new String();
            for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                data += DAFTAR_ANGGOTA.get(i).getIdAnggota() + "\t" + DAFTAR_ANGGOTA.get(i).getNama() + "\n";
            }
            try {
                outObjectStream.writeObject(data);
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                outStream.close();
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaFileAnggota() {
        try {
            FileInputStream inStream = null;
            String data = new String();
            File inFile = new File("Anggota.dat");
            try {
                inStream = new FileInputStream(inFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            int a;
            while ((a = inStream.read()) != -1) {
                data = data + (char) a;
            }
            inStream.close();
            System.out.println(data);
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void tambahTabelPublikasi(Publikasi publikasi) {
        Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314020", "MHS115314020");
        Statement statement = null;
        try {
            statement = conn.createStatement();
            String sql = "insert into publikasi (id_koleksi, judul) values ('" + publikasi.getIdKoleksi() + "','" + publikasi.getJudul() + "')";
            System.out.println(sql);
            statement.executeUpdate(sql);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void simpanTabelPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314020", "1MHS115314020");
            Statement statement = null;
            try {
                statement = conn.createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (int i = 0; i < DAFTAR_PUBLIKASI.size(); i++) {
                String sql = "insert into publikasi (id_koleksi, judul) values ('"
                        + DAFTAR_PUBLIKASI.get(i).getIdKoleksi() + "','"
                        + DAFTAR_PUBLIKASI.get(i).getJudul() + "')";
                try {
                    statement.executeUpdate(sql);
                } catch (SQLException ex) {
                    Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void tambahTabelAnggota(Anggota anggota) {
        try {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314020", "MHS115314020");
            java.sql.Statement statement = null;
            statement = conn.createStatement();
            String sql = "insert into anggota (id_anggota,nama) values ('" + anggota.getIdAnggota() + "','" + anggota.getNama() + "')";
            statement.executeUpdate(sql);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void simpanTabelAnggota() {
        if (DAFTAR_ANGGOTA.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Anggota Kosong");
        } else {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314020", "MHS115314020");
            try {
                java.sql.Statement statement = conn.createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                String sql = "insert into anggota (id_anggota,nama) values ('" + DAFTAR_ANGGOTA.get(i).getIdAnggota() + "','" + DAFTAR_ANGGOTA.get(i).getNama() + "')";
            }
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaTabelPublikasi() {
        DAFTAR_PUBLIKASI = new ArrayList<Publikasi>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314020", "MHS115314020");
        Statement statement = null;
        try {
            statement = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        String sql = "select * from publikasi";
        ResultSet result = null;
        try {
            result = statement.executeQuery(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while (result.next()) {
                Publikasi temp = new Publikasi() {
                    @Override
                    public int hitungDenda(int lamaPinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                };
                try {
                    temp.setIdKoleksi(result.getString("id_koleksi"));
                } catch (Exception ex) {
                    Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
                }
                temp.setJudul(result.getString("judul"));
                DAFTAR_PUBLIKASI.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void bacaTabelAnggota(){
       DAFTAR_ANGGOTA= new ArrayList<Anggota>();
       Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314020", "MHS115314020");
        java.sql.Statement statement = null;
        try {
            statement = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
       String sql = "select * from anggota";
       ResultSet result = null;
        try {
            result = statement.executeQuery(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while (result.next()) {
                Anggota temp = new Anggota() {

                    public int hitungDenda(int lamaPinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                };
                try {
                    temp.setIdAnggota(result.getString("id_anggota"));
                } catch (Exception ex) {
                    Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
                }
                temp.setNama(result.getString("nama"));
                DAFTAR_ANGGOTA.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
   }

    public static ArrayList<Anggota> cariAnggota(String nama) {
        ArrayList<Anggota> hasil = new ArrayList<Anggota>();
        for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
            Anggota temp = DAFTAR_ANGGOTA.get(i);
            if (temp.getNama().contains(nama)) {
                hasil.add(temp);
            }
        }
        if(hasil.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Data Tidak Ditemukan");
        }
        return hasil;
    }
    
    public void aturTimer(){
        
    }
    
}
